#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Description: 路由文件

from flask import Blueprint, render_template, request, flash

from app.models.user import User

bp = Blueprint('auth', __name__)


@bp.route('/', methods=['GET', 'POST'])
def login_1():
    return render_template('login.html')


@bp.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username'].strip()
        password = request.form['password'].strip()

        user = User()
        info = user.get_info()

        if info.get("username") == username and info.get("password") == password:
            flash('登录成功!', 'success')
            return render_template('index.html')
        else:
            error = '无效的用户名或密码'
            return render_template('login.html', error=error)

    return render_template('login.html')